//
//  IndexNewViewController.m
//  yigou
//
//  Created by SunChao on 17/3/6.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "IndexNewViewController.h"
#import "LgViewController.h"
#import "IndexLeftViewController.h"
#import "IndexRightViewController.h"
#import "ChooseSignViewController.h"

#define TableCount 2
#define TopViewWidth 150
#define TopViewHeight 40
#define padding 10
#define topMainViewHeight 40
#define slideViewFrame1 CGRectMake(10, TopViewHeight-2, TopViewWidth/2-20, 2)
#define slideViewFrame2 CGRectMake(TopViewWidth/2+10, TopViewHeight-2, TopViewWidth/2-20, 2)

@interface IndexNewViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong )UIScrollView *scrollView;

@property (nonatomic,strong) UIView *topView;

@property (strong, nonatomic) UIView *slideView;

@property (strong,nonatomic) IndexLeftViewController *leftVC;

@property (strong,nonatomic) IndexRightViewController *rightVC;

@property (strong,nonatomic)UITableView *tableView1;
@property (strong,nonatomic)UITableView *tableView2;

@property (nonatomic,strong)NSMutableArray *dataSource;


@end

@implementation IndexNewViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:POST_LOGINSUC object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:POST_LOGINSUC object:nil];
    [self initTopTabs];
    [self initScrollView];
    [self initDownTables];
    
    
}
- (void)TopViewCanSelceted{
    
    UIButton *button1 =(UIButton *)[self.topView viewWithTag:10];
    [button1 setEnabled:YES];
    UIButton *button2 =(UIButton *)[self.topView viewWithTag:11];
    [button2 setEnabled:YES];
}
- (void)TopViewCanNotSelceted{
    
    UIButton *button1 =(UIButton *)[self.topView viewWithTag:10];
    [button1 setEnabled:NO];
    UIButton *button2 =(UIButton *)[self.topView viewWithTag:11];
    [button2 setEnabled:NO];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    
    if (![UserData isLogin]) {
        LgViewController *loginViewController = [[LgViewController alloc]initWithNibName:@"LgViewController" bundle:nil];
        [self.navigationController pushViewController:loginViewController animated:NO];
    } else {
        if (![UserData isJoinTribe]) {
            ChooseSignViewController *loginViewController = [[ChooseSignViewController alloc]init];
            [self.navigationController pushViewController:loginViewController animated:NO];
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}


- (void) loginSuccess:(NSNotification *) notify {
    [_leftVC refresh];
    [_rightVC refresh];
}
#pragma mark -- 实例化顶部的tab
-(void) initTopTabs{
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TopViewWidth, TopViewHeight)];
    CGFloat width = TopViewWidth / TableCount;
    NSArray *titleArr = @[@"热门",@"关注"];
    for (int i = 0; i < TableCount; i ++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TopViewHeight)];
        
        view.backgroundColor = [UIColor clearColor];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TopViewHeight)];
        button.tag = i+10;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button setTitle:[titleArr objectAtIndex:i] forState:UIControlStateNormal];
        [button setFont:[UIFont systemFontOfSize:16]];
        [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        [_topView addSubview:view];
    }
    
    _slideView = [[UIView alloc] initWithFrame:slideViewFrame1];
    [_slideView setBackgroundColor:QINGColor];
    [_topView addSubview:_slideView];
    self.navigationItem.titleView = _topView;
}

#pragma mark -- 实例化ScrollView
-(void) initScrollView{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-40)];
    
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * TableCount, SCREEN_HEIGHT-64-40);
    //_scrollView.backgroundColor = [UIColor greenColor];
    
    _scrollView.pagingEnabled = YES;
    
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
}
#pragma mark --点击顶部的按钮所触发的方法
-(void) tabButton: (id) sender{
    
    UIButton *button = sender;
    if (button.tag ==10) {
        _slideView.frame =  slideViewFrame1;
    }
    if (button.tag == 11) {
        _slideView.frame = slideViewFrame2;
    }
    [_scrollView setContentOffset:CGPointMake((button.tag-10) * SCREEN_WIDTH, 0) animated:YES];
    NSLog(@"%lf,%ld",_scrollView.contentOffset.x,(long)button.tag);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"IndexNewVCTabButtonClicked" object:nil];
}

#pragma mark --初始化下方的TableViews
-(void) initDownTables{
    
    _leftVC = [[IndexLeftViewController alloc]init];
    [self addChildViewController:_leftVC];
    _leftVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49);
    [self.scrollView addSubview:_leftVC.view];
    
    _rightVC = [[IndexRightViewController alloc]init];
     [self addChildViewController:_rightVC];
    [self.scrollView addSubview:_rightVC.view];
    _rightVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49);
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == 0) {
        self.slideView.frame = slideViewFrame1;
    }else if (scrollView.contentOffset.x == SCREEN_WIDTH){
        self.slideView.frame = slideViewFrame2;
    }
}

@end
